<template>
	<view>
		<view class="goods-list">
			<view v-for="(goods,i) in goodsList" :key="i" @click="gotoDetail(goods)">	
				<view class="goods-item">
				<!-- 左侧盒子 -->	
				<view class="goods-item-left">
					<image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
				</view>
				<!-- 右侧盒子 -->
				<view class="goods-item-right">
					<view class="goods-name">
						<!-- 商品介绍 -->
						{{goods.goods_name}}
					</view>
					<view class="goods-info-box">
						<view class="goods-price">￥{{goods.goods_price | tofixed}}</view>
					</view>
					
				</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj:{
					query:'',
					cid:'',
					pagenum: 1,
					pagesize:15
				},
				goodsList:[],
				total:0,
				// 默认图片地址
				defaultPic:'https://static.botue.com/ugo/uploads/banner2.png',
			};
		},
		filters:{		
			// 过滤器处理商品价格
		  tofixed(num){ 
			return	Number(num).toFixed(2)
			}
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		methods:{
		async getGoodsList(cb){
		     const {data:res} =  await uni.$http.get('/api/public/v1/goods/search' , this.queryObj)
			 //关闭下拉刷新功能
			 cb && cb()
			 if(res.meta.status !== 200) return uni.$showMsg()
			 // 改造数据
			 // this.goodsList = res.message.goods
			 // 新旧数据的拼接
			 this.goodsList = [...this.goodsList,...res.message.goods]
			 this.total = res.message.total
			 
			},
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			}
		},
		onReachBottom() {
			// 若数据加载完毕，则不在发起请求
			if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据在加载完毕')
			// 上拉触底事件
			//1.让页码值自增加加1
			this.queryObj.pagenum++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			// 下拉刷新重置数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.goodsList = []
			// 重新发起数据请求
			this.getGoodsList(() => uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">
.goods-item{
	display: flex;
	padding: 10px 5px;
	border: 1px solid #efefef;
	
		.goods-item-left{
			margin-right: 5px;
			.goods-pic{
				width: 100px;
				height: 100px;
				display: block;
			}
		}		
		.goods-item-right{
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.goods-name{
				font-size: 13px;
			}
			.goods-info-box{
				.goods-price{
					font-size: 16px;
					color: #C00000;
				}
			}
		}
		}
</style>
